﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

using SharedGenomics.Core;
using SharedGenomics.Workbench.Core;
using SharedGenomics.Domain;
using SharedGenomics.Annotations;
using SharedGenomics.Workbench.AnnotationService;

namespace SharedGenomics.Workbench.Services
{
    /// <summary>
    /// The AnalysisResultsAnnotator fetches annotations for an <see cref="IAnalysisResultSet"/> i.e. the results of a job output. When all the annotations 
    /// for a particular result have been fetched, interested listeners are notified via the AnnotationSetCompleted event
    /// </summary>
    public class RemotingAnnotationSessionManager : WCFClientHost, IAnnotationSessionManager
    {
        private IAnnotationSessionService annotationSessionService;
        /// <summary>
        /// Creates a new instance of this object
        /// </summary>
        /// <param name="requestManager">The request manager is used to resolve all the active annotation requests used by the workbench</param>
        public RemotingAnnotationSessionManager(IAnnotationSessionService annotationSessionService)
        {
            this.annotationSessionService = annotationSessionService;
        }

        public AnnotationSessionWithDescriptionInfo[] GetAnnotationSessionsForUser()
        {
            //get the annotation sessions from the serivce - if no value is returned then return an empty enumeration
            var sessionInfosMaybe = this.annotationSessionService.GetAnnotationSessions();

            return sessionInfosMaybe.HasValue
                ? sessionInfosMaybe.Value
                : new AnnotationSessionWithDescriptionInfo[] { };
        }

        public void SetViewedAnnotationImportance(Guid viewedAnnotationId, bool isImportant)
        {
            this.annotationSessionService.SetViewedAnnotationImportance(viewedAnnotationId, isImportant);
        }

        public Guid CreateSession(int jobId)
        {
            Guid newSessionId = Guid.NewGuid();
            this.annotationSessionService.AddAnnotationSession(newSessionId, jobId);
            return newSessionId;
        }
    }
}
